package test.blued.com.casecontroller;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import test.blued.com.casecontroller.myInterface.HttpJson;

/**
 * Created by mm on 2017-11-17.
 */

public class InterfaceActivity extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.interface_layout);
        Intent intent = getIntent();
        String software = intent.getStringExtra("software");
        if(software.equals("home"))
            soft = 0;
        findView();
        init();
        button.setOnClickListener(this);
    }

    public void findView(){
        button = (Button)findViewById(R.id.button);
        online = (RadioButton)findViewById(R.id.international);
        test = (RadioButton)findViewById(R.id.home);
    }
    public void init(){
        online.setText("线上");
        online.setChecked(false);
        test.setText("测试");
        online.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    new Thread(){
                        @Override
                        public void run() {
                            hj = new HttpJson("post", authURL, onlineAuth, null);
                            hj.postString();
                            token = (String)hj.getResponseStrFile("data", "token");
                            Log.i("T11", "token-------" + token);
                        }
                    }.start();
                }
            }
        });
        test.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    new Thread(){
                        @Override
                        public void run() {
                            hj = new HttpJson("post", authURL, testAuth, null);
                            hj.postString();
                            token = (String)hj.getResponseStrFile("data", "token");
                            Log.i("T11", "token-------" + token);
                        }
                    }.start();
                }
            }
        });
        if((online.isChecked() && test.isChecked()) == false)
            button.setClickable(false);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button:
                InputStream in = getResources().openRawResource(R.raw.users);
                final BufferedReader usersBR = new BufferedReader(new InputStreamReader(in));
                new Thread(){
                    @Override
                    public void run() {
                        String str;
                        try {
                            while((str=usersBR.readLine())!=null){
                                String []interfaces = str.split("-");
                                String url;
                                if(interfaces[3].equals("get")){
                                    url = "https://argo.blued.cn"+interfaces[4];
                                    Log.i("T11", "-----------"+url);
                                    hj = new HttpJson("get", url, buildBaseHeader(token));
                                    hj.get();
                                    Log.i("T11", "---------------------------------");
                                }else{
                                    url =  "https://argo.blued.cn"+interfaces[4];
                                    Log.i("T11", "-----------"+url);
                                    hj = new HttpJson("post", url, interfaces[5], buildBaseHeader(token));
                                    hj.postString();
                                    Log.i("T11", "---------------------------------");
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            default:
                break;
        }
    }

    public static Map<String, String> buildBaseHeader(String token) {
        Map<String, String> baseHeaders = new ArrayMap<>();
        baseHeaders.put("Accept", "*/*");
        baseHeaders.put("Connection", "keep-alive");
        baseHeaders.put("Content-Type", "application/json");
        baseHeaders.put("Accept-Language", "zh-cn");
        baseHeaders.put("User-Agent", "Mozilla/5.0 (iPhone; iOS 11.0.3; Scale/2.00) iOS/310402_1.4.2_4331_057 (Asia/Shanghai) app/2");
        baseHeaders.put("Authorization", token);
        return baseHeaders;
    }

    private Button button;
    private RadioButton online;
    private RadioButton test;


    public HttpJson hj;
    private int soft = 1;          //标示国内-0， 国际-1
    public static String authURL = "http://qa.blued.cn/qatest/auth";
    private String onlineAuth = "{\"username\":\"m241055@yeah.net\",\"password\":\"mm1234\",\"online\":1}";
    private String testAuth = "{\"username\":\"ldq44@qq.com\",\"password\":\"1234\",\"online\":0}";
    private String token;
}
